
package cz.czu.pef.DataModelling.entityObjects;

import java.util.*;

import javax.persistence.*;

/**
 * This entity represents external worker, ie. someone who works for the company but is not its direct employee (contractor).
 * 
 * @author rstribrn
 *
 */
@Entity
@Table(name = "EXTERNAL_WORKER")
@Inheritance(strategy = InheritanceType.JOINED)
//@DiscriminatorValue("EMPLOYEE")
public class ExternalWorkerEntityImpl extends EmployeeEntityImpl implements IdPersistenceEntityObject
{
	private static final long serialVersionUID = -9003701092553813511L;

	/*
	 * Entity attributes (some inherited)
	 */

	/*
	 * Relations
	 */
	@ManyToMany(fetch = FetchType.EAGER, targetEntity = CompanyEntityImpl.class, mappedBy = "extWorkers") // no cascade!
	private Set<CompanyEntityImpl> externalEmployers;

	public ExternalWorkerEntityImpl()
	{
		super();
	}

	public ExternalWorkerEntityImpl(PersonEntityImpl person)
	{
		super(person);
	}

	public void addExternalEmployer(CompanyEntityImpl newEmployer)
	{
		if (externalEmployers == null)
			externalEmployers = new HashSet<CompanyEntityImpl>();
		externalEmployers.add(newEmployer);
	}

	public Set<CompanyEntityImpl> getExternalEmployers()
	{
		return externalEmployers;
	}

	public List<CompanyEntityImpl> getExternalEmployersAsList()
	{
		return new ArrayList<CompanyEntityImpl>(getExternalEmployers());
	}

    
  public void setExternalEmployers(Set<CompanyEntityImpl> externalEmployers)
  {
    this.externalEmployers = externalEmployers;
  }

    @Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("ExternalWorkerEntityImpl [toString()=");
		builder.append(super.toString());
		builder.append("]");
		return builder.toString();
	}
	
	/* (non-Javadoc)
	 * @see cz.czu.pef.DataModelling.domainObjects.ExternalWorker#canBeRemoved()
	 */
	@Override
	public boolean canBeRemoved()
	{
		return getExternalEmployers().size() == 0 && super.canBeRemoved();
	}
}